package com.wangshidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshidai.entity.SysPermission;
import com.wangshidai.mapper.SysRolePermissionMapper;
import com.wangshidai.reply.rolereply.QueryPermissionTreeReply;
import com.wangshidai.service.SysPermissionService;
import com.wangshidai.mapper.SysPermissionMapper;
import com.wangshidai.util.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Slf4j
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService{
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public List<QueryPermissionTreeReply> queryPermissionTree(long roleId) {
        List<QueryPermissionTreeReply> permissions = sysPermissionMapper.queryPermissionTree();
        log.info("所有权限:{}",permissions);
        List<Long> permissionIds = sysRolePermissionMapper.queryPermissionId(roleId);
        log.info("角色:{},拥有的有权限:{}",roleId,permissions);
        return QueryPermissionTreeReply.getPermissionTree(permissions,permissionIds);
    }



    /**
     * 查询所有权限信息
     * @return  sysPermissionList
     */
    @Override
    public List<SysPermission> querySysPerssionList() {
        List<SysPermission> sysPermissionList = sysPermissionMapper.querySysPerssionList();
        return sysPermissionList;
    }

    /**
     * 查询所有上级菜单
     * @return  sysPermissionList
     */
    @Override
    public List<SysPermission> queryPreviousMenu() {
        List<SysPermission> sysPermissionList = sysPermissionMapper.queryPreviousMenu();
        return sysPermissionList;
    }

    /**
     * 根据id查询权限信息
     * @param id
     * @return   sysPermission
     */
    @Override
    public SysPermission queryById(Integer id) {
        if (id == null) {
            try {
                throw new BusinessException("数据不存在");
            } catch (BusinessException e) {
                e.printStackTrace();
            }
        }
        SysPermission sysPermission = sysPermissionMapper.selectById(id);
        return sysPermission;
    }

    /**
     *  添加权限
     * @param sysPermission
     * @return   true
     * @throws BusinessException
     */
    @Override
    public Boolean savePermission(SysPermission sysPermission) {
        log.info("{}",sysPermission);
        //假如从持久层返回的数据不为空，提示数据已重复
        if (sysPermissionMapper.savePermission(sysPermission) != null) {
            //throw  new BusinessException("数据已重复");
            return false;
        }//否则调用持久层方法进行添加并返回
        sysPermissionMapper.insert(sysPermission);
        return true;
    }

    /**
     * 修改权限
     * @param sysPermission
     * @return  true
     * @throws BusinessException
     */
    @Override
    public Boolean updatePermission(SysPermission sysPermission) {
        log.info("{}",sysPermission);
        //如果对象为空，则提示数据不合法
        //调用方法进行修改
        sysPermissionMapper.updateById(sysPermission);
        return true;
    }

    /**
     * 根据id删除数据
     * @param id
     * @throws BusinessException
     */
    @Override
    public void deletePermission(Integer id)  {
        log.info("{}",id);
        //判断父权限是否有子权限
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id",id);
        //调用方法统计count
        Long count = sysPermissionMapper.selectCount(wrapper);
        //如果count>0说明有子类，提示不能删除
        if (count >0) {
            try {
                throw new BusinessException("有子类，无法删除");
            } catch (BusinessException e) {
                e.printStackTrace();
            }
        }
    }

}




